<?php

App::uses('Component', 'Controller');
App::uses('GatewayFlowComponent', PAYMENT_CAMELCASENAME.'.Component');
require_once 'GatewayFlowComponent.php';

class DepositoContaComponent extends Component implements GatewayFlowComponent{
	
	var $components = array('Session');
	var $data;
//	var $transaction_url = 'https://ws.pagseguro.uol.com.br/v2/checkout';
//	var $notification_url = 'https://ws.pagseguro.uol.com.br/v2/transactions/notifications/';
	
	public function config($id){
		$model = ClassRegistry::init(PAYMENT_CAMELCASENAME.'.PaymentGateway');
		return $model->config($id);
	}
	
	public function setUpTransaction($id, $reference, $items){
		$config = $this->config($id);
		$this->data['config'] = $config;
		$this->data['id'] = $id;
		$this->data['reference'] = $reference;
		$this->data['items'] = $items;
		$this->data['amount'] = 0;
		foreach($items as $item){
			$this->data['amount'] += $item['amount'];
		}
		$this->Pagamento = ClassRegistry::init('Pagamento');
		$this->Pagamento->id = $reference;
		$this->Pagamento->save(array('hash'=>'DEPOSITO_EM_CONTA'));
	}
	
	
	public function sendTransaction(){
		$result['code'] = 'DEPOSITO_EM_CONTA';
		$result['date'] = date('Y-m-d H:i:s');
		$result['display'] = 'deposito_conta';
		$result['data'] = $this->data;
		$result['message'] = 'OK';
		return $result;;
	}
	
	public function notification($data, $id){
		return array(
			'notification_code'=>$id,
			'notification_type'=>'DEPOSITO_EM_CONTA',
			'new'=>1,
			'redirect'=>array('plugin'=>false, 'controller'=>'usuarios', 'action'=>'index')
		);
	}
	
	public function getNotification($id, $gateway){
		$result = new stdClass();
		$result->reference = $id;
		$result->code = 'DEPOSITO_EM_CONTA';
		$result->status = 2;
		$result->paymentMethod->type = 1;
		$result->paymentMethod->code = null;
		$this->Pagamento = ClassRegistry::init('Pagamento');
		$pagamento = $this->Pagamento->findById($id);
		$result->grossAmount = $pagamento['Pagamento']['valor_total'];
		return $result;
	}
	
}
